package com.site.wisdom_construction.controller.iotModule;

import com.alibaba.fastjson.JSONObject;
import com.site.wisdom_construction.responseEntity.JsonResult;
import com.site.wisdom_construction.responseEntity.OneDataResult;
import com.site.wisdom_construction.responseEntity.Page;
import com.site.wisdom_construction.service.iotModule.TowerCraneService;
import com.site.wisdom_construction.util.HttpRequest;
import com.site.wisdom_construction.util.verification.NoToken;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author daiyifei
 * @Date 2021/12/23
 * @Description
 */
@Api(tags = "塔吊数据接口")
@RestController
@RequestMapping("TowerCrane")
@CrossOrigin
public class TowerCraneController {

    @Autowired
    private TowerCraneService towerCraneService;

    @ApiOperation(value = "塔吊基础数据", notes = "")
    @PostMapping("/findTowerCraneBasics")
    @NoToken
    public OneDataResult findTowerCraneBasics() throws InterruptedException {
        OneDataResult oneDataResult = new OneDataResult();
        JSONObject jsonObject = new JSONObject();
        HttpRequest httpRequest =new HttpRequest();
        String path = "http://gdw-sz.taantech.com:9090/TowerService/Basic";
        jsonObject = httpRequest.StringData(path,"imei=TAKJ_T_20212076&apikey=%7B8D46E497-E6EB-473A-B38B-7288598CB13E%7D");
        oneDataResult.setData(jsonObject);
        return oneDataResult;
    }

    @ApiOperation(value = "塔吊实时数据", notes = "")
    @PostMapping("/findTowerCraneRealTime")
    @NoToken
    public OneDataResult findTowerCraneRealTime() throws InterruptedException {
        OneDataResult oneDataResult = new OneDataResult();
        JSONObject jsonObject = new JSONObject();
        HttpRequest httpRequest =new HttpRequest();
        String path = "http://gdw-sz.taantech.com:9090/TowerService/Realtime";
        jsonObject = httpRequest.StringData(path,"imei=TAKJ_T_20212076&apikey=%7B8D46E497-E6EB-473A-B38B-7288598CB13E%7D");
        oneDataResult.setData(jsonObject);
        return oneDataResult;
    }

    @ApiOperation(value = "塔吊报警数据", notes = "")
    @PostMapping("/findTowerCraneAlarm")
    @NoToken
    public OneDataResult findTowerCraneAlarm() throws InterruptedException {
        OneDataResult oneDataResult = new OneDataResult();
        JSONObject jsonObject = new JSONObject();
        HttpRequest httpRequest =new HttpRequest();
        String path = "http://gdw-sz.taantech.com:9090/TowerService/Alarm";
        jsonObject = httpRequest.StringData(path,"imei=TAKJ_T_20212076&apikey=%7B8D46E497-E6EB-473A-B38B-7288598CB13E%7D");
        oneDataResult.setData(jsonObject);
        return oneDataResult;
    }

    @ApiOperation(value = "删除塔吊实时数据", notes = "测试数据:")
    @PostMapping("/deleteTowerCrane")
    public JsonResult deleteCarWash(@RequestBody Map map){
        JsonResult jsonResult = new JsonResult();
        List<Integer> list = (List<Integer>) map.get("list");
        int l = 0;
        Map mapR = new HashMap();
        for (int i = 0;i<list.size();i++){
            mapR.put("id",list.get(i));
            int a = towerCraneService.deleteTowerCrane(mapR);
            l+=a;
        }
        if (l>=1){
            jsonResult.setCode(200);
            jsonResult.setMessage("成功删除塔吊实时数据!");
        }else {
            jsonResult.setCode(20006);
            jsonResult.setMessage("删除塔吊实时数据失败!");
        }
        return jsonResult;
    }

    @ApiOperation(value = "分页模糊查询塔吊实时数据", notes = "测试数据:")
    @PostMapping("/findTowerCraneByPage")
    public Page findTowerCraneByPage(@RequestBody Map map) {
        Page page = new Page();
        page.setPageNo((Integer) map.get("pageNo"));
        page.setPageSize((Integer) map.get("pageSize"));
        page.setTotal(towerCraneService.Total(map));
        page.setItems(towerCraneService.findTowerCraneByPage(map));
        return page;
    }
}
